package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	5695
 * http://www.cqcb.com/  重庆晨报
 *
 * @author KK
 *
 */
public class CqcbNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		/*
		*		[0-9]*
		*       [a-z]*
		*		[a-z0-9]*
		*       [a-zA-Z0-9]*
		*/

		"http://www.reyou.cn/news/[0-9]*.html"
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String title=null;
		String content=null;
		Long date=null;
		String author=null;
		Document doc=Jsoup.parse(htmltxt);
		news.setUrl(url);

		title=doc.select("div.model_a h2").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("title").text();
			if (title!=null)
				title=StringUtils.substringBefore(title, "-");
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.con clear"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("span.zt12"));
		}
		date=DateUtils.matchDate(doc.select("div.t_fu").text());
		if (date == null) {
			date=DateUtils.matchDate(doc.select("div.edit").text());
			if (date == null) {
				date=DateUtils.matchDate(doc.select("div.picteb").text());
			}
		}

		author =JSoupUtils.matchAuthor(doc, "编辑：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
		news.setAuthor(author);
		news.setType(1);
		return news;
	}
	public static void main(String[] args) {
		String url="http://www.reyou.cn/news/41722.html";
		CqcbNewsAnalyse analyse=new CqcbNewsAnalyse();
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
